﻿/*
Copyright © 2010 François Karman

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

See <license.txt> for the complete license of the software.
*/

using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Threading;

namespace Faz.Dependencies
{
    /// <summary>
    /// The entry point of the application.
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// Initializes a new instance of the App class.
        /// </summary>
        public App()
        {
            this.DispatcherUnhandledException += this.OnDispatcherUnhandledException;
            this.InitializeComponent();
        }

        /// <summary>
        /// The entry point of the application.
        /// </summary>
        [STAThread]
        public static void Main()
        {
            App app = new App();
            app.Run();
        }

        /// <summary>
        /// Called on application startup - loads the main window.
        /// </summary>
        /// <param name="e">The parameter is not used.</param>
        protected override void OnStartup(StartupEventArgs e)
        {
            Window window = Application.LoadComponent(new Uri("MainWindow.xaml", UriKind.RelativeOrAbsolute)) as Window;

            MainPresenter presenter = new MainPresenter();
            window.DataContext = presenter;
            window.Show();

            base.OnStartup(e);
        }

        /// <summary>
        /// Called on application exit - saves the user settings.
        /// </summary>
        /// <param name="e">The parameter is not used.</param>
        protected override void OnExit(ExitEventArgs e)
        {
            Faz.Dependencies.Properties.Settings.Default.Save();
            base.OnExit(e);
        }

        /// <summary>
        /// Called when an exception is forwarded to the application - logs the exception.
        /// </summary>
        /// <param name="sender">The parameter is not used.</param>
        /// <param name="e">The details about the unhandled exception.</param>
        private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            Trace.Default.TraceEvent(TraceEventType.Critical, 1, "An unhandled exception was raised <{0}-{1}>", e.Exception.GetType().Name, e.Exception.Message);
            e.Handled = true;
        }
    }
}
